<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="@/assets/images/ziji.jpg" alt class="hearImg"/>
        个人练习的电商后台管理系统
      </div>
      <el-button type="info" @click="quit">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 左侧区域 -->
      <el-aside :style="{ width: fold ? '64px' : '250px' }">
        <div class="fold" @click="foldBtn">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#4296f7"
          unique-opened
          :collapse="fold"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menusList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/home/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="maintain('/home/' + subItem.path)"
            >
              <template slot="title">
                <i class="iconfont icon-users"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 导入左侧菜单请求
import { getMenusApi } from '@/api/menus.js'
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单数据
      menusList: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-3702mima',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      fold: false,
      // 是否高亮
      activePath: ''
    }
  },
  created() {
    // 获取左侧菜单数据
    getMenusApi().then((res) => {
      if (res.data.meta.status === 200) {
        this.menusList = res.data.data
      } else {
        this.$message.error(res.data.meta.msg)
      }
    })
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    quit() {
      sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    foldBtn() {
      this.fold = !this.fold
    },
    // 保持高亮
    maintain(activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang='less' scoped>
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  padding-left: 0;
  justify-content: space-between;
  background-color: #373d41;
  align-items: center;
  color: #fff;
  font-size: 20px;
  img {
    vertical-align: middle;
    margin-right: 10px;
  }
}

.el-aside {
  background-color: #333744;
  // width: 200px !important ;
  .el-menu {
    border-right: none;
  }
  .fold {
    background-color: #495266;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 3px;
    cursor: pointer;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.hearImg{
  width: 61px;
  height: 59px;
}
</style>
